<?php


define('ACCESS_KEY9296',true);
require('./include/init.php');


//if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{




if($_GET['m']=='reg'){
    $arr['username'] = $_POST['user'];
    $arr['password'] = md5($_POST['password']);
    $arr['number'] = $_POST['number'];
    $user=$arr['username'];
    $password1 = $_POST['password1'];
    $password = $_POST['password'];
    $account = new AccountModel();
    $mysql = mysql::getIns();

    if($mysql->getOne("SELECT * FROM account WHERE username = '{$user}'")){
        echo '用户名已存在';
        exit;
    }
    else if($password!==$password1){
        echo '两次密码不同';
        exit;
    }
    else if($mysql->getOne("SELECT * FROM account WHERE number = {$arr['number']}")){
        echo '该电话号码已存在';
        exit;
    }
    if($account->reg($arr)){
//$_SESSION['user']=$arr['username'];
        echo '注册成功';
    }
}
else if($_GET['m']=='login'){
    $arr['username'] = $_POST['user'];
    $arr['password'] = md5($_POST['password']);
    $user=$arr['username'];
    $password = $arr['password'];
    $account = new AccountModel();
    if(@$_SESSION['user']==$arr['username']){
        $res['a']='1';
        $res['b']='你已经登录，不需要重复登录';
    header("Content-Type:text/html; charset=utf-8");
    exit(json_encode($res));
    }
    if(!($user_id=$account->login("SELECT id FROM account WHERE username = '{$user}' AND password = '$password'"))){
        $res['a']='0';
        $res['b']='登录失败';
        header("Content-Type:text/html; charset=utf-8");
        exit(json_encode($res));
    }
    $_SESSION['user']=$arr['username'];
    $_SESSION['id']=$user_id;
    $res['a']='1';
    header("Content-Type:text/html; charset=utf-8");
    exit(json_encode($res));
}
else if($_GET['m']=='out'){
    session_unset();
    session_destroy();
    $res['a']='1';
    header("Content-Type:text/html; charset=utf-8");
    exit(json_encode($res));
}
else if($_GET['m']=='islogin'){
    if(@$_SESSION['user']){
        $res['a']='1';
        $res['b']=$_SESSION['user'];
        header("Content-Type:text/html; charset=utf-8");
        exit(json_encode($res));
    }
    else{
        $res['a']='0';
        header("Content-Type:text/html; charset=utf-8");
        exit(json_encode($res));
    }
}

}
/*else {
echo 'error';
}*/